/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package aptech.semester.code.data;

import aptech.semester.code.common.ApplicationCommon;
import aptech.semester.code.conn.MyLib;
import aptech.semester.code.entity.EmployeeEntity;
import aptech.semester.code.entity.EmployeeSalaryEntity;
import java.io.FileInputStream;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author vi
 */
public class EmployeeSalaryList {
    
    public List<EmployeeSalaryEntity> getEmployeeSalaryBySalaryForm(int salaryFormId) {
        List<EmployeeSalaryEntity> listEmp = new ArrayList<EmployeeSalaryEntity>();
        
        try {
            //1.tao 1 ket noi
            Connection cn = MyLib.getCN();
            if(cn!=null) {
            String sql = "SELECT * FROM EMPLOYEE_SALARY WHERE SALARY_FORM_ID = ?";
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setInt(1, salaryFormId);
            //3.cho thi hanh lenh truy van tren doi tuong nay va ket qua se dc tra ve doi tuong resulltset
            ResultSet rs = pst.executeQuery();
            
            //4. duyuet ket qua tra ve trong resultset rs va fill vao arraylist ds.
            while(rs.next()) {
                EmployeeSalaryEntity empSalary = new EmployeeSalaryEntity();
                empSalary.setEmployeeSalaryId(rs.getInt("ID"));
                empSalary.setEmployeeId(rs.getString("EMPLOYEE_ID"));
                empSalary.setDayOffNoSalary(rs.getFloat("DAYOFF_NO_SALARY"));
                empSalary.setDayOffInspect(rs.getFloat("DAYOFF_INSPECT"));
                empSalary.setBonusPlus(rs.getBigDecimal("BONUS_PLUS"));
                empSalary.setBonusSub(rs.getBigDecimal("BONUS_SUB"));
                empSalary.setTotalSalary(rs.getBigDecimal("TOTAL_SALARY"));
                empSalary.setSalaryForm(rs.getInt("SALARY_FORM_ID"));
                empSalary.setOvertime(rs.getInt("OVERTIME"));
                empSalary.setWorkingDate(rs.getInt("WORKING_DAY"));
                
                listEmp.add(empSalary);
            }
            rs.close();
            cn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return listEmp;
    }
    
    public EmployeeSalaryEntity getEmployeeSalaryByFormAndEmpId(int salaryFormId, String employeeId) {
        EmployeeSalaryEntity empSalary = new EmployeeSalaryEntity();
        
        try {
            //1.tao 1 ket noi
            Connection cn = MyLib.getCN();
            if(cn!=null) {
            String sql = "SELECT * FROM EMPLOYEE_SALARY WHERE SALARY_FORM_ID = ? AND EMPLOYEE_ID = ?";
            PreparedStatement pst = cn.prepareStatement(sql);
            pst.setInt(1, salaryFormId);
            pst.setString(2, employeeId);
            //3.cho thi hanh lenh truy van tren doi tuong nay va ket qua se dc tra ve doi tuong resulltset
            ResultSet rs = pst.executeQuery();
            
            //4. duyuet ket qua tra ve trong resultset rs va fill vao arraylist ds.
            while(rs.next()) {
                
                empSalary.setEmployeeSalaryId(rs.getInt("ID"));
                empSalary.setEmployeeId(rs.getString("EMPLOYEE_ID"));
                empSalary.setDayOffNoSalary(rs.getFloat("DAYOFF_NO_SALARY"));
                empSalary.setDayOffInspect(rs.getFloat("DAYOFF_INSPECT"));
                empSalary.setBonusPlus(rs.getBigDecimal("BONUS_PLUS"));
                empSalary.setBonusSub(rs.getBigDecimal("BONUS_SUB"));
                empSalary.setTotalSalary(rs.getBigDecimal("TOTAL_SALARY"));
                empSalary.setSalaryForm(rs.getInt("SALARY_FORM_ID"));
                empSalary.setOvertime(rs.getInt("OVERTIME"));
                empSalary.setWorkingDate(rs.getInt("WORKING_DAY"));
                
                
            }
            rs.close();
            cn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return empSalary;
    }
    
    public boolean insertListEmployeeSalary(List<EmployeeSalaryEntity> listEmpSalary) {
        
        try {
            //1. tao ket noi
            Connection cn = MyLib.getCN();
            
            //2.tao doi tuong lenh insert sql
            String sql = "INSERT EMPLOYEE_SALARY values(?,?,?,?,?,?,?,?,?)";
            PreparedStatement pst = cn.prepareStatement(sql);
            for (EmployeeSalaryEntity empSalary : listEmpSalary) {
                pst.setString(1, empSalary.getEmployeeId());
                pst.setFloat(2, empSalary.getDayOffNoSalary());
                pst.setFloat(3, empSalary.getDayOffInspect());
                pst.setBigDecimal(4, empSalary.getBonusPlus());
                pst.setBigDecimal(5, empSalary.getBonusSub());
                pst.setBigDecimal(6, empSalary.getTotalSalary());
                pst.setInt(7, empSalary.getSalaryForm());
                pst.setInt(8, empSalary.getOvertime());
                pst.setInt(9, empSalary.getWorkingDate());
                pst.executeUpdate();
            }
            pst.close();
            cn.close();
            return true;
            
        } catch (Exception e) {
            e.printStackTrace();
            return false;
            
        } 
    }
}
